package com.wu.controller;/**
 * @author sun
 * @date 2022/3/21 - 0:18
 */

import com.wu.pojo.Images;
import com.wu.pojo.Labels;
import com.wu.pojo.MaterialsValues;
import com.wu.pojo.Products;
import com.wu.pojo.vo.MaterialsVo;
import com.wu.pojo.vo.ProductVo;
import com.wu.service.*;
import com.wu.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: TODO
 * @author
 * @date 2022/3/21 0:18
 * @version 1.0
 */
@RestController
@Api(tags="Products模块")
@CrossOrigin
@RequestMapping("/products")
public class ProductsController {
    @Autowired
    private ProductsService productsService;
    @Autowired
    private ImagesService imagesService;
    @Autowired
    private LabelsService labelsService;
    @Autowired
    private MaterialsService materialsService;
    @Autowired
    private MaterialsValuesService materialsValuesService;

    @PostMapping("/insertProducts")
    @ApiOperation(value="增加Products")
    /*@Transactional(rollbackFor = Exception.class)*/
    public R insertProducts(Products products){
        try {
            productsService.insertSelective(products);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }

    @PostMapping("/insertProductsVo")
    @ApiOperation(value="增加ProductsVo")
    public R insertProductsVo(ProductVo productVo){
        try {
            productsService.insertSelective(productVo);
                    if (productVo.getImagesList()!=null &&productVo.getImagesList().size()>0){
                        List<Images> imagesList = productVo.getImagesList();
                        for (Images images : imagesList) {
                            imagesService.insertSelective(images);
                        }
                    }
                    if (productVo.getLabelsList()!=null&& productVo.getLabelsList().size()>0){
                        List<Labels> labelsList = productVo.getLabelsList();
                        for (Labels labels : labelsList) {
                            labelsService.insertSelective(labels);
                        }
                    }
                    if (productVo.getMaterialsVoList()!=null && productVo.getMaterialsVoList().size()>0){
                        List<MaterialsVo> materialsVoList = productVo.getMaterialsVoList();
                        for (MaterialsVo materialsVo : materialsVoList) {
                            materialsService.insertSelective(materialsVo);
                            List<MaterialsValues> materialsValuesList = materialsVo.getMaterialsValuesList();
                            if (materialsValuesList!=null &&materialsValuesList.size()>0){
                                for (MaterialsValues materialsValues : materialsValuesList) {
                                    materialsValuesService.insertSelective(materialsValues);
                                }
                            }
                        }
                    }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }


    @GetMapping("/deleteProducts/{id}")
    @ApiOperation(value="删除Products")
    /*@Transactional(rollbackFor = Exception.class)*/
    public R deleteProducts(Integer id){
        try {
            productsService.deleteByPrimaryKey(id);
            /*productsRedirectcontentService.deleteByProductsId(id);*/
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PutMapping("/updateProducts")
    @ApiOperation(value="更新Products")
    /*@Transactional(rollbackFor = Exception.class)*/
    public R updateProducts(@RequestBody Products products){
        try {
            productsService.updateByPrimaryKeySelective(products);
           /* productsRedirectcontentService.updateByPrimaryKeySelective(productsRedirectcontent);*/
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findProductsList")
    @ApiOperation(value="查询ProductsList")
    public R findProducts(Products products){
        try {
            List<ProductVo> list= productsService.findProductsVo(products);
            return R.ok().data("productsList",list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findProductsById/{id}")
    @ApiOperation(value="通过id查询Products")
    public R findProductsById(Integer id){
        try {
            Products products= productsService.selectByPrimaryKey(id);
            return R.ok().data("products",products);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

}
